<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Department List</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 20px;
            color: #333;
        }

        .container {
            max-width: 1000px;
            margin: 0 auto;
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        h1 {
            text-align: center;
            color: #2c3e50;
            margin-bottom: 30px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        th {
            background-color: #3498db;
            color: white;
            font-weight: bold;
        }

        tr:nth-child(even) {
            background-color: #f2f2f2;
        }

        tr:hover {
            background-color: #e3f2fd;
        }

        .loading {
            text-align: center;
            padding: 20px;
            font-style: italic;
            color: #666;
        }

        .error {
            color: #e74c3c;
            text-align: center;
            padding: 20px;
            font-weight: bold;
        }

        .refresh-btn {
            display: block;
            margin: 20px auto;
            padding: 10px 20px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        .refresh-btn:hover {
            background-color: #2980b9;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>Department List</h1>
    <button class="refresh-btn" onclick="fetchDepartments()">Refresh Data</button>
    <div id="loading" class="loading">Loading department data...</div>
    <div id="error" class="error" style="display: none;"></div>
    <table id="departmentTable" style="display: none;">
        <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>SN</th>
        </tr>
        </thead>
        <tbody id="tableBody">
        <!-- Data will be inserted here -->
        </tbody>
    </table>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        fetchDepartments();
    });

    function fetchDepartments() {
        const loadingElement = document.getElementById('loading');
        const errorElement = document.getElementById('error');
        const tableElement = document.getElementById('departmentTable');
        const tableBody = document.getElementById('tableBody');

        // Show loading, hide table and error
        loadingElement.style.display = 'block';
        errorElement.style.display = 'none';
        tableElement.style.display = 'none';

        fetch('/department/list')
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                return response.json();
            })
            .then(data => {
                console.log(data)
                if (data.success) {
                    // Clear previous data
                    tableBody.innerHTML = '';
                    // Populate table with new data
                    data.data.forEach(dept => {
                        const row = document.createElement('tr');
                        row.innerHTML = `
                                <td>${dept.id}</td>
                                <td>${dept.name}</td>
                                <td>${dept.sn}</td>
                            `;
                        tableBody.appendChild(row);
                    });

                    // Show table, hide loading
                    tableElement.style.display = 'table';
                    loadingElement.style.display = 'none';
                } else {
                    throw new Error(data.msg || 'Unknown error occurred');
                }
            })
            .catch(error => {
                loadingElement.style.display = 'none';
                errorElement.textContent = 'Error: ' + error.message;
                errorElement.style.display = 'block';
                console.error('Error fetching department data:', error);
            });
    }
</script>
</body>
</html>